<?php

namespace App\Http\Controllers\Backend\Repay;

use App\Http\Requests\Backend\Repay\RepayRequest;
use App\Library\Tools\ExcelExport;
use App\Models\RepaySummary;
use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\UserCertOcr;

class RepayController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:backend');
    }

    public function getStatus(){
        $fullStatus       = RepaySummary::$FULL_STATUS;
        $fullStatusDetail = RepaySummary::$FULL_STATUS_DETAIL;
        $info             = [];
        foreach ($fullStatus as $k => $v) {
            $tmp = [];
            foreach ($fullStatusDetail[$k] as $key=>$value){
                $tmp[] = [
                    'id' => $key,
                    'name' => $value
                ];
            }
            $info[] = [
                'status'   => [
                    'id' => $k,
                    'name' => $v
                ],
                'detail_status' => $tmp
            ];
        }
        return success($info);

    }

    /**
     * Description: 获取放款成功用户列表
     * Author:lijiafei
     */
    public function getRepayInfo(RepayRequest $request){
        $status = $request->get('status','');
        $type = $request->get('type','');
        $name = $request->get('name','');
        $page = $request->get('page',1);
        $page_size = $request->get('page_size',20);
        $data = RepaySummary::getRepayList($status,$type,$name,$page_size);
        return success($data);
    }

    /**
     * Description:用户详情
     * Author:lijiafei
     */
    public function getUserInfo(RepayRequest $request){
        $uid = $request->get('uid');
        $page = $request->get('page',1);
        $page_size = $request->get('page_size',10);
        if(!$uid){
            return failed('请传递用户标识');
        }
        $data = RepaySummary::getUserInfo($uid,$page_size);
        return success($data);
    }


    /**
     * Description:前端放款用户的导出
     * Author:lijiafei
     * @param RepayRequest $request
     */
    public function repayExport(RepayRequest $request){
        $status = $request->get('status','');
        $type = $request->get('type','');
        $name = $request->get('name','');
        $data = RepaySummary::getRepayListExport($status,$type,$name);
        $header = ['借款状态','细分状态','历史借次','渠道来源','用户id','姓名','手机号码','loanid','放款金额','期数','身份证号','收款日期','应还日期'];
        $fields = ['summary_status','type','count','source','uid','name','phone_num','loan_id','amount','period_count','num','repay_date','normal_date'];
        $info = new ExcelExport($data,$header,$fields,'repay.xlsx');
        $info->export();
    }
}
